<?php 
namespace Home\Controller;

use Think\Controller;

class OrderinfoController extends Controller
{
	public function index()
	{
		$uid = $_SESSION['User']['uid'];
		if(empty($uid)){
			$this->error('请先登录');
		}

	// 以下为所有商品↓

		//查询该用户的所有订单
		$ord = M('order');
		$blist = $ord->field('ordernum,otime,price,sendstatus,paystatus')->where(array('uid'=>$uid))->select();
		// var_dump($blist);
		//存储订单下的商品数据
		$slist = [];
		// 查出所有订单下的商品
		foreach ($blist as $k => $v) {
			$slist[$k] = $ord->field('orderdata.gid,orderdata.price,orderdata.num,orderdata.img,orderdata.gname')->join('orderdata on order.ordernum = orderdata.ordernum')->where(array('orderdata.ordernum'=>$v['ordernum']))->select();
		}
		// 合并
		foreach ($blist as $k => $v) {
			$blist[$k]['arr'] = $slist[$k];
		}
		
	// 以下为未支付商品↓

		$nopay = [];
		$slists = [];
		
		$nopay = $ord->field('id,ordernum,otime,price,sendstatus,paystatus')->where(array('uid'=>$uid,'paystatus'=>0))->select();
				// var_dump($nopay) ;
		//存储订单下的商品数据
		// $slists;
		// 查出所有订单下的商品
		foreach ($nopay as $k => $v) {
			$slists[$k] = $ord->field('orderdata.gid,orderdata.price,orderdata.num,orderdata.img,orderdata.gname')->join('orderdata on order.ordernum = orderdata.ordernum')->where(array('orderdata.ordernum'=>$v['ordernum']))->select();
		}
		// var_dump($slists);
		// 合并
		foreach ($nopay as $k => $v) {
			$nopay[$k]['arr'] = $slists[$k];
		}
		// var_dump($nopay);die;

	// 以下为待发货商品↓	

		$nosend = [];
		$slists = [];
		
		$nosend = $ord->field('ordernum,otime,price,sendstatus,paystatus')->where(array('uid'=>$uid,'sendstatus'=>0))->select();
		// var_dump($nosend			var_dump($nosend) ;die;
		//存储订单下的商品数据
		$slists;
		// 查出所有订单下的商品
		foreach ($nosend as $k => $v) {
			$slists[$k] = $ord->field('orderdata.gid,orderdata.price,orderdata.num,orderdata.img,orderdata.gname')->join('orderdata on order.ordernum = orderdata.ordernum')->where(array('orderdata.ordernum'=>$v['ordernum']))->select();
		}
		// 合并
		foreach ($nosend as $k => $v) {
			$nosend[$k]['arr'] = $slists[$k];
		}

	// 以下为待收货商品↓	

		$noreceive = [];
		$slists = [];
		
		$noreceive = $ord->field('id,ordernum,otime,price,sendstatus,paystatus')->where(array('uid'=>$uid,'sendstatus'=>1))->select();
		// var_dump($noreceive			var_dump($noreceive) ;die;
		//存储订单下的商品数据
		$slists;
		// 查出所有订单下的商品
		foreach ($noreceive as $k => $v) {
			$slists[$k] = $ord->field('orderdata.gid,orderdata.price,orderdata.num,orderdata.img,orderdata.gname')->join('orderdata on order.ordernum = orderdata.ordernum')->where(array('orderdata.ordernum'=>$v['ordernum']))->select();
		}
		// 合并
		foreach ($noreceive as $k => $v) {
			$noreceive[$k]['arr'] = $slists[$k];
		}

	// 以下为待评价商品↓	

		$nocomment = [];
		$slists = [];
		
		$nocomment = $ord->field('ordernum,otime,price,sendstatus,paystatus')->where(array('uid'=>$uid,'sendstatus'=>2,'paystatus'=>1))->select();
		// var_dump($nocomment			var_dump($nocomment) ;die;
		//存储订单下的商品数据
		$slists;
		// 查出所有订单下的商品
		foreach ($nocomment as $k => $v) {
			$slists[$k] = $ord->field('orderdata.gid,orderdata.price,orderdata.num,orderdata.img,orderdata.gname')->join('orderdata on order.ordernum = orderdata.ordernum')->where(array('orderdata.ordernum'=>$v['ordernum']))->select();
		}
		// 合并
		foreach ($nocomment as $k => $v) {
			$nocomment[$k]['arr'] = $slists[$k];
		}
	

		// var_dump($nopay);die;
		// var_dump($slist);
		// var_dump($blist);
		// echo 'all';
		// var_dump($slists[0]);
		// var_dump($noreceive);
		// var_dump($slist1);
		// var_dump($blist1);
		// echo 1;
		// var_dump($slist2);
		// var_dump($blist2);
		// echo 2;
		//订单内的商品
		$this->assign('slists',$slists);
		//全部的订单加商品
		$this->assign('blist',$blist);
		//全部未支付的商品
		$this->assign('nopay',$nopay);
		//全部待发货的商品
		$this->assign('nosend',$nosend);
		//全部待收货的商品
		$this->assign('noreceive',$noreceive);
		//全部待评价的商品
		$this->assign('nocomment',$nocomment);
		// var_dump($blist);
		$this->display();
	}

	public function orderback()
	{
		$this->display();
	}

	//付钱
	public function dopay()
	{
		// var_dump($_GET);
		$id = $_GET['id'];
		$paystatus = 1;
		$list = [];
		$list['paystatus']=$paystatus;
		$ord = M('order');
		if($ord->create($list)){
			if($ord->where('id='.$id)->save()){
				$this->success('付款成功!!!');
			}else{
				$this->error('操作失败');
			}
		}
	}

	//收货
	public function doreceiver()
	{
		// var_dump($_GET);die;
		if($_GET['paystatus']==0){
			$this->error('请先去付款!!!');
			die;
		}
		$id = $_GET['id'];
		$sendstatus = 2;
		$list = [];
		$list['sendstatus']=$sendstatus;
		$ord = M('order');
		if($ord->create($list)){
			if($ord->where('id='.$id)->save()){
				$this->success('收货成功');
			}else{
				$this->error('操作失败');
			}
		}
		// var_dump($list);
		
	}

	//将订单内的商品遍历到评论页面
	public function commentlist()
	{
		$ordernum = $_GET['ordernum'];
		// var_dump($ordernum);die;
		$orda = M('orderdata');
		$list = $orda->where(array('ordernum' => $ordernum))->select();

		// var_dump($list);
		// die;
		$this->assign('list',$list);
		$this->display('Orderinfo/commentlist');
		// echo 111;
	}
	// 做评论
	public function docomment()
	{
		// var_dump($_POST);die;
		$uid = $_SESSION['User']['uid'];
		$com = M('comment');
		$list = $_POST;
		$time = date('Y-m-d H:i:s');
		// var_dump($time);
		$list['uid'] = $uid;
		$list['time'] = $time;
		
		if($result=$com->create($list)){
			// var_dump($result);die;
			if($com->add()){
				// echo 1;
				$this->success("评论成功!");
			}else{
				// echo 2;
				$this->error("评论失败!");
			}
		}
		
	}

}